package com.nine.system.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nine.common.entity.ChangeForm;
import com.nine.common.entity.ChangeInfoVO;
import com.nine.common.entity.EngineerForm;
import com.nine.common.entity.engineerVO.EngineerAddress;
import com.nine.common.tool.ResponseResult;
import com.nine.common.tool.SnowflakeIdGenerator;
import com.nine.system.mapper.ChangeInfoMapper;
import com.nine.system.mapper.UserInfoMapper;
import com.nine.system.mapper.EngineerMapper;
import com.nine.system.service.EngineerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * @Description:
 * @ClassName: EngineerServiceImpl
 * @Author: luodeng
 * @Date: 2023/6/6 10:38
 * @Version: 1.0
 */
@Service
public class EngineerServiceImpl implements EngineerService {

    @Autowired
    private EngineerMapper mapper;

    @Autowired
    private ChangeInfoMapper changeInfoMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;


    //获取所有工程师
    @Override
    public ResponseResult getAll( int page, int limit) {
        IPage<EngineerForm> EngineerPage = new Page<>(page,limit);
        IPage<EngineerForm> engineerFormIPage = mapper.selectPage(EngineerPage, null);
        List<EngineerForm> engineers = engineerFormIPage.getRecords();
        long count = engineerFormIPage.getTotal();

        return ResponseResult.okc(engineers,count);
    }

    @Override
    public ResponseResult getOne(Long id) {
        LambdaQueryWrapper<EngineerForm> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EngineerForm::getEngineerId,id);

        EngineerForm engineerForm = mapper.selectOne(wrapper);
        return ResponseResult.ok(engineerForm);
    }

    @Override
    public ResponseResult getSome(String engineerName,
                                  String engineerProvince,
                                  String engineerCity,
                                  String engineerArea,
                                  String engineerPhone,
                                  int page,
                                  int limit) {
        LambdaQueryWrapper<EngineerForm> wrapper = new LambdaQueryWrapper<>();
        IPage<EngineerForm> iPage = new Page<>(page,limit);


        if (engineerName != null && !engineerName.isEmpty()) {
            wrapper.eq(EngineerForm::getEngineerName, engineerName);
        }

        if (engineerProvince != null && !engineerProvince.isEmpty()) {
            wrapper.eq(EngineerForm::getEngineerProvince, engineerProvince);
        }

        if (engineerCity != null && !engineerCity.isEmpty()) {
            wrapper.eq(EngineerForm::getEngineerCity, engineerCity);
        }
        if (engineerArea != null && !engineerArea.isEmpty()) {
            wrapper.eq(EngineerForm::getEngineerArea, engineerArea);
        }
        if (engineerPhone != null && !engineerPhone.isEmpty()) {
            wrapper.eq(EngineerForm::getEngineerPhone, engineerPhone);
        }

        IPage<EngineerForm> iPage1 = mapper.selectPage(iPage, wrapper);
        List<EngineerForm> records = iPage1.getRecords();
        long total = iPage1.getTotal();


        return ResponseResult.okc(records,total);
    }
    @Override
    public ResponseResult updateEngineerIsEnabled(Long id, Integer isEnabled) {
        EngineerForm engineer = mapper.selectById(id);
        engineer.setEngineerIsEnabled(String.valueOf(isEnabled));

        mapper.updateById(engineer);
        return ResponseResult.ok("更新engineer_is_enabled成功");
    }

    //工程师修改个人信息业务
    @Override
    public ResponseResult changeInfo(ChangeInfoVO changeInfoVO) {

        ChangeForm changeForm = new ChangeForm();
        changeForm.setChangePhone(changeInfoVO.getEngineerPhone());
        changeForm.setChangeProvince(changeInfoVO.getEngineerProvince());
        changeForm.setChangeCity(changeInfoVO.getEngineerCity());
        changeForm.setChangeArea(changeInfoVO.getEngineerArea());
        changeForm.setChangeReason(changeInfoVO.getChangeReason());
        changeForm.setChangeDescription(changeInfoVO.getEngineerDescription());
        long uid = StpUtil.getLoginIdAsLong();
        changeForm.setEngineerId(uid);
        changeInfoMapper.insert(changeForm);

        return ResponseResult.ok("提交变更成功");
    }

    //通过token获取到uid
    @Override
    public ResponseResult getAddressByToken() {

        long uid = StpUtil.getLoginIdAsLong();
        EngineerAddress engineerAddress = mapper.getEngineerAddress(uid);

        return ResponseResult.ok(engineerAddress);
    }

    @Override
    public ResponseResult getMyInfo() {
        int engineerId = StpUtil.getLoginIdAsInt();
        EngineerForm engineerForm = mapper.selectById(engineerId);

        return ResponseResult.ok(engineerForm);
    }
}
